package cn.edu.neusoft.zengweidong707.neufood.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsSuccessBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class UpdateUserInfoActivity extends BaseActivity {
    private EditText c_username;
    private EditText c_userpass;
    private EditText c_mobilenum;
    private EditText c_address;
    private EditText c_comment;
    private Button c_btn_back;
    private Button c_btn_ok;
    private UserModel model;
    private ZwdListener<IsSuccessBean> listener;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_update_user_info);
        c_username = (EditText) findViewById(R.id.c_username);
        c_userpass = (EditText) findViewById(R.id.c_userpass);
        c_mobilenum = (EditText) findViewById(R.id.c_mobilenum);
        c_address = (EditText) findViewById(R.id.c_address);
        c_comment = (EditText) findViewById(R.id.c_comment);
        c_btn_back = (Button) findViewById(R.id.c_btn_return);
        c_btn_ok = (Button) findViewById(R.id.c_btn_ok);
    }

    @Override
    void initEvent() {
        listener = new ZwdListener<IsSuccessBean>() {
            @Override
            public void onResponse(IsSuccessBean l) {
                if("1".equals(l.getSuccess())) {
                    showToast("修改个人信息成功,请重新登陆!");
                    Intent intent = new Intent(UpdateUserInfoActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    showToast("修改个人信息失败");
                }
            }

            @Override
            public void onFail(String msg) {
                showToast("请检查网络是否连接");
            }
        };

        c_btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        c_btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = c_username.getText().toString();
                String address = c_address.getText().toString();
                String mobilenum = c_mobilenum.getText().toString();
                String userpass = c_userpass.getText().toString();
                String uid = getUserid();
                model = new UserModel();
                model.updateUserInfo(Integer.parseInt(uid.toString()), username, userpass, mobilenum, address, listener);
            }
        });
    }

    @Override
    void initData() {
        Bundle bundle = getIntent().getExtras();
        c_username.setText(bundle.getString("username"));
        //c_userpass.setText(bundle.getString("userpass"));
        c_mobilenum.setText(bundle.getString("mobilenum"));
        c_address.setText(bundle.getString("address"));
        c_comment.setText(bundle.getString("comment"));
    }


}
